//
//  JKRFeedStatusLayout.m
//  JKRFeedDemo
//
//  Created by Lucky on 2017/12/10.
//  Copyright © 2017年 Lucky. All rights reserved.
//

#import "JKRFeedStatusLayout.h"

@implementation JKRFeedStatusLayout

- (instancetype)initWithModel:(JKRFeedDataModel *)model {
    self = [super init];
    
    if (model.order.order_type == 1) { // 陪玩
        switch (model.order.order_status) {
            case 1: // 已发布
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = YES;
                    NSString *topString = @"等待老板买单";
                    NSString *bottomString = model.order.tracking.desc;
                    
                    NSMutableAttributedString *topAttr = [[NSMutableAttributedString alloc] initWithString:topString attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14], NSForegroundColorAttributeName:UIColorHex(7F7F7F)}];
                    NSMutableAttributedString *bottomAttr = [[NSMutableAttributedString alloc] initWithString:bottomString attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12], NSForegroundColorAttributeName:UIColorHex(FFA718)}];
                    
                    YYTextContainer *container = [YYTextContainer containerWithSize:CGSizeMake(kScreenWidth - 30, MAXFLOAT)];
                    container.maximumNumberOfRows = 1;
                    self.topLabelLayout = [YYTextLayout layoutWithContainer:container text:topAttr];
                    self.bottomLabelLayout = [YYTextLayout layoutWithContainer:container text:bottomAttr];
                    self.topLabelFrame = CGRectMake((kScreenWidth - self.topLabelLayout.textBoundingRect.size.width) * 0.5, 10, self.topLabelLayout.textBoundingRect.size.width, self.topLabelLayout.textBoundingRect.size.height);
                    self.bottomLabelFrame = CGRectMake((kScreenWidth - self.bottomLabelLayout.textBoundingRect.size.width) * 0.5, 40, self.bottomLabelLayout.textBoundingRect.size.width, self.bottomLabelLayout.textBoundingRect.size.height);
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            case 4: // 超时无人购买
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = YES;
                    NSString *topString = @"等待老板买单";
                    NSString *bottomString = model.order.tracking.desc;
                    
                    NSMutableAttributedString *topAttr = [[NSMutableAttributedString alloc] initWithString:topString attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14], NSForegroundColorAttributeName:UIColorHex(7F7F7F)}];
                    NSMutableAttributedString *bottomAttr = [[NSMutableAttributedString alloc] initWithString:bottomString attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12], NSForegroundColorAttributeName:UIColorHex(FFA718)}];
                    
                    YYTextContainer *container = [YYTextContainer containerWithSize:CGSizeMake(kScreenWidth - 30, MAXFLOAT)];
                    container.maximumNumberOfRows = 1;
                    self.topLabelLayout = [YYTextLayout layoutWithContainer:container text:topAttr];
                    self.bottomLabelLayout = [YYTextLayout layoutWithContainer:container text:bottomAttr];
                    self.topLabelFrame = CGRectMake((kScreenWidth - self.topLabelLayout.textBoundingRect.size.width) * 0.5, 10, self.topLabelLayout.textBoundingRect.size.width, self.topLabelLayout.textBoundingRect.size.height);
                    self.bottomLabelFrame = CGRectMake((kScreenWidth - self.bottomLabelLayout.textBoundingRect.size.width) * 0.5, 40, self.bottomLabelLayout.textBoundingRect.size.width, self.bottomLabelLayout.textBoundingRect.size.height);
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            case 6: // 老板买单
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = NO;
                } else if([model.order.payer.uid isEqualToString:CurrentUserID]) { // 老板
                    self.show = NO;
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            case 7: // 超时未填写结果
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = NO;
                } else if([model.order.payer.uid isEqualToString:CurrentUserID]) { // 老板
                    self.show = NO;
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            case 8: // 填写结果-打了
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = NO;
                } else if([model.order.payer.uid isEqualToString:CurrentUserID]) { // 老板
                    self.show = NO;
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            case 9: // 填写结果-没打
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = NO;
                } else if([model.order.payer.uid isEqualToString:CurrentUserID]) { // 老板
                    self.show = NO;
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            case 10: // 填写打了超时未确认或者确认服务
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = NO;
                } else if([model.order.payer.uid isEqualToString:CurrentUserID]) { // 老板
                    self.show = NO;
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            case 11: // 发起投诉
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = NO;
                } else if([model.order.payer.uid isEqualToString:CurrentUserID]) { // 老板
                    self.show = NO;
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            case 12: // 申诉成功
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = NO;
                } else if([model.order.payer.uid isEqualToString:CurrentUserID]) { // 老板
                    self.show = NO;
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            case 13: // 申诉失败
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = NO;
                } else if([model.order.payer.uid isEqualToString:CurrentUserID]) { // 老板
                    self.show = NO;
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            default:
                break;
        }
    } else if (model.order.order_type == 2) { // 约玩
        switch (model.order.order_status) {
            case 1: // 已发布
            {
                BOOL isSignUp = NO;
                for (JKRFeedOrderCandidate *candidate in model.order.candidates) {
                    if ([candidate.uid isEqualToString:CurrentUserID]) {
                        isSignUp = YES;
                        break;
                    }
                }
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    if (model.order.candidates.count > 0) {
                        self.show = NO;
                    } else {
                        self.show = YES;
                        
                        NSString *topString = @"暂时还没有大神报名噢！";
                        NSString *bottomString = model.order.tracking.desc;
                        
                        NSMutableAttributedString *topAttr = [[NSMutableAttributedString alloc] initWithString:topString attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14], NSForegroundColorAttributeName:UIColorHex(7F7F7F)}];
                        NSMutableAttributedString *bottomAttr = [[NSMutableAttributedString alloc] initWithString:bottomString attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12], NSForegroundColorAttributeName:UIColorHex(FFA718)}];
                        
                        YYTextContainer *container = [YYTextContainer containerWithSize:CGSizeMake(kScreenWidth - 30, MAXFLOAT)];
                        container.maximumNumberOfRows = 1;
                        self.topLabelLayout = [YYTextLayout layoutWithContainer:container text:topAttr];
                        self.bottomLabelLayout = [YYTextLayout layoutWithContainer:container text:bottomAttr];
                        self.topLabelFrame = CGRectMake((kScreenWidth - self.topLabelLayout.textBoundingRect.size.width) * 0.5, 10, self.topLabelLayout.textBoundingRect.size.width, self.topLabelLayout.textBoundingRect.size.height);
                        self.bottomLabelFrame = CGRectMake((kScreenWidth - self.bottomLabelLayout.textBoundingRect.size.width) * 0.5, 40, self.bottomLabelLayout.textBoundingRect.size.width, self.bottomLabelLayout.textBoundingRect.size.height);
                    }
                } else  { // 报名者 游客
                    if (model.order.candidates.count > 0) {
                        self.show = YES;
                        NSMutableAttributedString *voteUserString = [NSMutableAttributedString new];
                        
                        NSInteger maxCount = ((NSInteger)kScreenWidth - 200) / 25;
                        
                        NSInteger showCount = maxCount < model.order.candidates.count ? (maxCount - 1) : model.order.candidates.count;
                        for (NSInteger i = 0; i < showCount; i ++) {
                            JKRFeedOrderCandidate *userModel = model.order.candidates[i];
                            NSAttributedString *pic = [NSAttributedString circleHeader_attributedStringWithFontSize:20 imageURL:userModel.thumbnail shrink:NO];
                            YYTextHighlight *highLight = [YYTextHighlight new];
                            highLight.userInfo = @{kFeedTopClickUser:userModel.uid};
                            NSMutableAttributedString *picM = [[NSMutableAttributedString alloc] initWithAttributedString:pic];
                            [picM setTextHighlight:highLight range:picM.string.rangeOfAll];
                            [voteUserString appendAttributedString:picM];
                            [voteUserString appendAttributedString:[[NSMutableAttributedString alloc] initWithString:@" "]];
                        }
                        
                        if (maxCount < model.order.candidates.count) {
                            NSAttributedString *pic = [NSAttributedString original_attributedStringWithFontSize:20 image:[JKRFeedDetailHelper imageNamed:@"more_fxsy"] shrink:NO];
                            [voteUserString appendAttributedString:pic];
                            YYTextHighlight *highLight = [YYTextHighlight new];
                            highLight.userInfo = @{kFeedTopClickCandidateList:kFeedTopClickCandidateList};
                            [voteUserString setTextHighlight:highLight range:NSMakeRange(voteUserString.length - pic.length, pic.length)];
                        }
                        
                        YYTextContainer *container = [YYTextContainer containerWithSize:CGSizeMake(kScreenWidth - 200, MAXFLOAT)];
                        self.topLabelLayout = [YYTextLayout layoutWithContainer:container text:voteUserString];
                        self.topLabelFrame = CGRectMake(15, (64 - self.topLabelLayout.textBoundingRect.size.height) * 0.5, self.topLabelLayout.textBoundingRect.size.width, self.topLabelLayout.textBoundingRect.size.height);
                        
                        NSMutableAttributedString *tail = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%zd位大神已加入接单", model.order.candidates.count]];
                        tail.font = [UIFont systemFontOfSize:11];
                        tail.color = UIColorHex(A7ABB4);
                        
                        {
                            YYTextContainer *container = [YYTextContainer containerWithSize:CGSizeMake(200 - 30, MAXFLOAT)];
                            container.maximumNumberOfRows = 1;
                            self.bottomLabelLayout = [YYTextLayout layoutWithContainer:container text:tail];
                            self.bottomLabelFrame = CGRectMake(kScreenWidth - 15 - self.bottomLabelLayout.textBoundingRect.size.width, (64 - self.bottomLabelLayout.textBoundingRect.size.height) * 0.5, self.bottomLabelLayout.textBoundingRect.size.width, self.bottomLabelLayout.textBoundingRect.size.height);
                        }
                        
                    } else {
                        self.show = NO;
                    }
                }
            }
                break;
            case 2: // 超时无人报名
            {
                BOOL isSignUp = NO;
                for (JKRFeedOrderCandidate *candidate in model.order.candidates) {
                    if ([candidate.uid isEqualToString:CurrentUserID]) {
                        isSignUp = YES;
                        break;
                    }
                }
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = YES;
                    
                    NSString *topString = @"暂时还没有大神报名噢！";
                    NSString *bottomString = model.order.tracking.desc;
                    
                    NSMutableAttributedString *topAttr = [[NSMutableAttributedString alloc] initWithString:topString attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14], NSForegroundColorAttributeName:UIColorHex(7F7F7F)}];
                    NSMutableAttributedString *bottomAttr = [[NSMutableAttributedString alloc] initWithString:bottomString attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12], NSForegroundColorAttributeName:UIColorHex(FFA718)}];
                    
                    YYTextContainer *container = [YYTextContainer containerWithSize:CGSizeMake(kScreenWidth - 30, MAXFLOAT)];
                    container.maximumNumberOfRows = 1;
                    self.topLabelLayout = [YYTextLayout layoutWithContainer:container text:topAttr];
                    self.bottomLabelLayout = [YYTextLayout layoutWithContainer:container text:bottomAttr];
                    self.topLabelFrame = CGRectMake((kScreenWidth - self.topLabelLayout.textBoundingRect.size.width) * 0.5, 10, self.topLabelLayout.textBoundingRect.size.width, self.topLabelLayout.textBoundingRect.size.height);
                    self.bottomLabelFrame = CGRectMake((kScreenWidth - self.bottomLabelLayout.textBoundingRect.size.width) * 0.5, 40, self.bottomLabelLayout.textBoundingRect.size.width, self.bottomLabelLayout.textBoundingRect.size.height);
                } else {
                    self.show = NO;
                }
            }
                break;
            case 3: // 超时未选大神
            {
                BOOL isSignUp = NO;
                for (JKRFeedOrderCandidate *candidate in model.order.candidates) {
                    if ([candidate.uid isEqualToString:CurrentUserID]) {
                        isSignUp = YES;
                        break;
                    }
                }
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = NO;
                } else  { // 报名者 游客
                    self.show = NO;
                }
            }
                break;
            case 5: // 选择大神
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = NO;
                } else if ([model.order.candidate.uid isEqualToString:CurrentUserID]) { // 大神
                    self.show = NO;
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            case 7: // 超时未填写结果
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = NO;
                } else if ([model.order.candidate.uid isEqualToString:CurrentUserID]) { // 大神
                    self.show = NO;
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            case 8: // 填写结果-打了
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = NO;
                } else if ([model.order.candidate.uid isEqualToString:CurrentUserID]) { // 大神
                    self.show = NO;
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            case 9: // 填写结果-没打
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = NO;
                } else if ([model.order.candidate.uid isEqualToString:CurrentUserID]) { // 大神
                    self.show = NO;
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            case 10: // 填写打了超时未确认或者确认服务
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = NO;
                } else if ([model.order.candidate.uid isEqualToString:CurrentUserID]) { // 大神
                    self.show = NO;
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            case 11: // 发起投诉
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = NO;
                } else if ([model.order.candidate.uid isEqualToString:CurrentUserID]) { // 大神
                    self.show = NO;
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            case 12: // 申诉成功
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = NO;
                } else if ([model.order.candidate.uid isEqualToString:CurrentUserID]) { // 大神
                    self.show = NO;
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            case 13: // 申诉失败
            {
                if ([model.feed.author.uid isEqualToString:CurrentUserID]) { // 发布者
                    self.show = NO;
                } else if ([model.order.candidate.uid isEqualToString:CurrentUserID]) { // 大神
                    self.show = NO;
                } else { // 游客
                    self.show = NO;
                }
            }
                break;
            default:
                break;
        }
        
    }
    
    return self;
}

@end
